package com.sjzsqjy.asdtiang.util;

public class Test {
	public static void main(String args[]){
		String a="我是abc";
		String b=a.substring(0,a.indexOf("是"));
		System.out.println(a.length()+b);
		System.out.println();
		String str="我爱祖国XXX，XXX上红旗飘。";
        System.out.println(str.substring(0,1));
		System.out.println(getSubString(str, 0, 2));

//		结果是：我


//		String str="我Love祖国XXX，XXX上红旗飘。";
//		getSubString(str, 0, 3)

//		结果是：我L

	}
	/**
	 * 截取字符串
	 * @param str 字符串
	 * @param begin 开始位置
	 * @param end  结束位置
	 * @return 
	 */
	public static String getSubString(String str, int begin, int end)
	{
		int count = 0;
		int offset = 0;
		char[] c = str.toCharArray();
		System.out.println(c);

		for (int i = 0; i < c.length; i++)
		{
			if (c[i] > 256)
			{
				offset = 2;
				count += 2;
			}
			else
			{
				offset = 1;
				count++;
			}
			if (count == end)
				return str.substring(begin, i + 1);
	
			if ((count == end + 1 && offset == 2))
				return str.substring(begin, i);	
		}
		return "";
	}

}
